<template>
    <div>
         <xjsearch></xjsearch>
        <van-nav-bar title="我的兑换课程" left-text="返回" left-arrow @click-left="onClickLeft" />
        <van-cell title="我的课程"/>
         <van-pull-refresh v-model="refreshing" @refresh="onRefresh()">
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad()" :immediate-check="false">
            <van-card :title="item.title" :desc="item.description" :thumb="item.cover" v-for="(item, index) in list" :key="index" @click="go(item.id,item.info_type,item.type,item.start_time,item.end_time)">
                <template #bottom>
                <div class="size">学习时间</div>
                <div class="size">{{item.start_time}}-{{item.end_time}}</div>
                </template>
           </van-card>
         </van-list>
        </van-pull-refresh>
    </div>
</template>
<script>
import xjsearch from '@/components/shop/xjsearch.vue';
export default {
    components: {
			xjsearch
		},
   data() {
    return {
            courselist: '',
            loading: true,
            finished: false,
            refreshing: false,
            total: '',
            list:[],
            page:1,
            searchdata:""
        };
   },
  methods: {
    onClickLeft() {
      this.$router.push({name:'my'});
    },
    async getListCourse() {
      let query = {
                page: 1,
                limit: 10,
                keyword: ''
            };
      query.page = this.page;
      let res=await this.$frontaxios.post(this.$front.getListCourse,query);
          if(res.code==1){
              this.loading= true;
              this.courselist=res.data.list;
              this.total= res.data.total;
          }else{
              console.log('没有获取到兑换课程')
          }
    },
    async onLoad(){
            if (this.refreshing) {
                this.list = [];
                this.refreshing = false;
            }
            await this.getListCourse();
            this.list.push(...this.courselist);
            this.page++;
            this.loading = false; //不加载了，滚动底会变为true
            if (this.list.length >= this.total) {
                this.finished = true; //数据全部加载完设为true
            }
        },
    onRefresh(){
            // 清空列表数据
            this.finished = false;
            // 重新加载数据
            // 将 loadingall 设置为 true，表示处于加载状态
            this.loading = true;
            this.page= 1;
            this.onLoad();
            this.$toast.success('刷新成功');
    },
    go(id,info_type,type,start_time,end_time){
          let starttime=getHTime(start_time);
          let endtime=getHTime(end_time);
          let nowtime=Date.now();
          if(nowtime<starttime||nowtime>endtime){
              this.$dialog.alert({title:"温馨提示",message:"使用期限未到或已到"});
              return;
          }
          if(info_type=='knowledge'){
               if(type=='video'){
                     this.$router.push({ name: 'videocontent', query: { id,columnid:'dhcourse'}});
               }else if(type=='audio'){
                     this.$router.push({ name: 'audiocontent', query: { id,columnid:'dhcourse'}});
               }else if(type=='image_text'){
                     this.$router.push({ name: 'image_textcontent', query: { id,columnid:'dhcourse'}});
               }
          }else if(info_type=='column'){
                     this.$router.push({name:"product",query:{id,action:'dh'}});
          }else if(info_type=='ExamPaper'){
                     this.$router.push({ name:'examcontent',query:{id,columnid:'dhcourse'}});
          }
    }
  },
  created() {
        this.onLoad();
    }
}
</script>
<style  scoped>
.size{
  font-size: 10px;
  color: rgb(160, 157, 157);
}
</style>
